<?php

namespace App\Admin\Controllers;

use App\Models\PrinterAdmin;
use App\Models\Shop;
use App\Tools\CouldPrint;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class PrinterController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(PrinterAdmin::class, function (Grid $grid) {
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('shop.shop_name', '商铺名');
                $filter->like('machinecode', '终端号');
            });
            $grid->id('ID')->sortable();
            $grid->column('shop.shop_name', '商铺名');
            $grid->column('machinecode', '终端号');
            $grid->column('department', '部门');
            $grid->column('cuisine', '菜系分类');
            $grid->column('priter_number', '打印联数');
            $grid->is_useall('是否全部打印')->display(function ($is_useall){
                switch ($is_useall){
                    case '0':
                        return '是';
                        break;
                    case '1':
                        return '否';
                        break;
                    default:
                        return '是';
                        break;
                }
            });
            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(PrinterAdmin::class, function (Form $form) {

            $form->saving(function (Form $form) {
                $shop = Shop::find($form->shop_id);
                if(!$shop){
                    echo "<script> alert('店铺不存在！');history.back(); </script>";die;
                }
                $cloudPrint = new CouldPrint();
                $count = PrinterAdmin::where('machinecode',$form->machinecode)->count();
                if ($count>0){
                    echo "<script> alert('已存在，添加打印机失败！');history.back(); </script>";die;
                }
                $add_res = $cloudPrint->addPrint($form->machinecode,$form->machinekey);
                if ($add_res['error'] != '0'){
                    echo "<script> alert('添加打印机失败！');history.back(); </script>";die;
                }
                if($form->is_useall=='on'){
                    $form->is_useall=0;
                }else{
                    $form->is_useall=1;
                }
                $res = PrinterAdmin::create(['shop_id'=>$form->shop_id,'machinekey'=>$form->machinekey,'machinecode'=>$form->machinecode,'department'=>$form->department,'is_useall'=>$form->is_useall]);
                if (!$res){
                    echo "<script> alert('添加打印机失败！');history.back(); </script>";die;
                }
                echo "<script> alert('添加打印机成功！');history.back();</script>";die;
            });

            $form->display('id', 'ID');
            $form->text('shop_id', '店铺id');
            $form->text('department', '部门');
            $is_useall = [
                'on' => ['value' => 0, 'text' => '是', 'color' => 'primary'],
                'off'  => ['value' => 1, 'text' => '否', 'color' => 'default'],
            ];
            $form->switch('is_useall', '是否全部打印')->states($is_useall);
            $form->text('machinecode', '终端号');
            $form->password('machinekey', '密钥');
            $form->text('priter_number', '打印联数');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
